package com.eien.phonezwei.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.method.NumberKeyListener;
import android.widget.EditText;
import android.widget.Toast;

import com.eien.phonezwei.R;

public class MainActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

	private SharedPreferences ePrefs;
	private EditTextPreference numPref;
	// private SwitchPreference contactOnlyPref;
	private SwitchPreference missedCallPref;
	private SwitchPreference forwardSmsPref;
	private EditText editText;

	@Override
	@SuppressWarnings("deprecation")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.setting_preference);
		ePrefs = PreferenceManager.getDefaultSharedPreferences(this);
		numPref = (EditTextPreference) findPreference("sendToNumber");
		editText = numPref.getEditText();
		// contactOnlyPref = (SwitchPreference) findPreference("contactOnly");
		missedCallPref = (SwitchPreference) findPreference("forwardMissedCall");
		forwardSmsPref = (SwitchPreference) findPreference("forwardSMS");
		checkNumber();
		ePrefs.registerOnSharedPreferenceChangeListener(this);
		editText.setFilters(new InputFilter[] { new InputFilter() {
			@Override
			public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
				/* 这里可以作输入字符串替换的功能，如输入的是密码，可以把所有输入变成"*"号 */
				return null;
			}
		}, new InputFilter.LengthFilter(11) /* 这里限制输入的长度为5个字母 */
		});

		editText.setKeyListener(new NumberKeyListener() {
			@Override
			public int getInputType() {
				return InputType.TYPE_MASK_VARIATION;
			}

			@Override
			protected char[] getAcceptedChars() {
				return new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; // 这里实现字符串过滤，把你允许输入的字母添加到下面的数组即可！
			}
		});
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
		if (key.equals("sendToNumber")) {
			// System.out.println(prefs.getString(key, ""));
			checkNumber();
		} else if (key.equals("forwardMissedCall")) {
			if (prefs.getBoolean("forwardMissedCall", false)) {
				if (checkStringInt(prefs.getString("sendToNumber", "")) && !prefs.getString("sendToNumber", "").equals("")) {
					ShowToast("未接来电通知服务已启动");
				} else {
					ShowToastLong("手机号未设置或者格式有误，请先设置手机号");
					SharedPreferences.Editor mEditor = prefs.edit();
					mEditor.putBoolean("forwardMissedCall", false);
					mEditor.putBoolean("forwardSMS", false);
					mEditor.commit();
					missedCallPref.setChecked(false);
					forwardSmsPref.setChecked(false);
				}
			} else {
				ShowToast("未接来电通知服务已关闭");
			}
		} else if (key.equals("forwardSMS")) {
			if (prefs.getBoolean("forwardSMS", false)) {
				if (checkStringInt(prefs.getString("sendToNumber", "")) && !prefs.getString("sendToNumber", "").equals("")) {
					ShowToast("短信转发服务已启动");
				} else {
					ShowToastLong("手机号未设置或者格式有误，请先设置手机号");
					SharedPreferences.Editor mEditor = prefs.edit();
					mEditor.putBoolean("forwardMissedCall", false);
					mEditor.putBoolean("forwardSMS", false);
					mEditor.commit();
					missedCallPref.setChecked(false);
					forwardSmsPref.setChecked(false);
				}
			} else {
				ShowToast("短信转发服务已关闭");
			}
		} else if (key.equals("contactOnly")) {
			if (prefs.getBoolean("contactOnly", false)) {
				ShowToast("已设置 转发过滤");
			} else {
				ShowToast("已取消 转发过滤");
			}
		}
	}

	private void checkNumber() {
		if (ePrefs.getString("sendToNumber", "").equals("")) {
			numPref.setSummary("请设置短信接收方的手机号");
		} else {
			if (checkStringInt(ePrefs.getString("sendToNumber", "")) && !ePrefs.getString("sendToNumber", "").equals("")) {
				numPref.setSummary("短信接收方手机号：" + ePrefs.getString("sendToNumber", ""));
			} else {
				numPref.setSummary("输入的手机号格式错误，请重新输入手机号");
				numPref.setText("");
				missedCallPref.setChecked(false);
				forwardSmsPref.setChecked(false);
			}
		}
	}

	boolean checkStringInt(String str) {
		String result = "";
		String reg = "[^\\d\\.]";
		result = eMatch(str, reg);
		if (!"".equals(result)) {
			ShowToast("手机号格式错误，只能为数字，不能含有" + result);
			return false;
		} else {
			return true;
		}
	}

	public String eMatch(String str, String reg) {
		Pattern P = Pattern.compile(reg);
		Matcher M = P.matcher(str);
		String result = "";
		if (M.matches()) {
			return "";
		} else {
			while (M.find()) {
				result += M.group();
			}
			return result;
		}
	}

	public void ShowToastLong(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}

	public void ShowToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
}
